﻿using SiteApp.BLL.Common;
using SiteApp.BLL.Interface;
using SiteApp.DAL;
using SiteApp.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace SiteApp.BLL
{
    public class NewInfraredService : INewInfraredService
    {
        public int InsertNewInfrared(NewInfrared model)
        {
            var result = SqlDBHelper.Execute($"insert into  [NewInfrared](TempId,PointName,DeviceSn,Time,IsAlarm,ImgBase64,AllTemp,AlarmMsg,CREATETIME) values(@TempId,@PointName,@DeviceSn,@Time,@IsAlarm,@ImgBase64,@AllTemp,@AlarmMsg,@CREATETIME)", model);
            return result;
        }

        public List<WebInfrared> NewInfrareForDate(string point, string startTime, string endTime)
        {
            var result = new List<WebInfrared>();
            if (!string.IsNullOrWhiteSpace(point) && !string.IsNullOrWhiteSpace(startTime) && !string.IsNullOrWhiteSpace(endTime))
            {
                var newInfraredList = new List<NewInfrared>();
                var newInfraredDetailsList = new List<NewInfraredDetails>();
                var device = SqlDBHelper.Query<SqlPoint, NewInfrared, NewInfraredDetails, SqlPoint>($"select * from  [Point] as a inner join NewInfrared as b on a.SID = b.DeviceSn left join NewInfraredDetails as c on b.TempId = c.TempId   where a.SID = {point}  and(b.[Createtime] >= '{startTime}' or    b.[Createtime] < '{endTime}')",
                    (pointEntity, newInfrared, newInfraredDetails) =>
                    {
                        if (newInfraredList.Where(a => a.TempId == newInfrared.TempId).Count() <= 0)
                        {
                            newInfraredList.Add(newInfrared);
                        }

                        newInfraredDetailsList.Add(newInfraredDetails);
                        return pointEntity;
                    },
                   splitOn: "SID,SID").ToList();//设备信息

                for (int i = 0; i < newInfraredList.Count; i++)
                {

                    var model = new WebInfrared();

                    //设备以及电站信息
                    var pointModel = device.Where(d => d.SID == Convert.ToInt32(point)).FirstOrDefault();
                    if (pointModel != null)
                    {
                        model.Point = pointModel.Point;
                        model.Voltage = pointModel.Voltage;
                        model.Device = pointModel.Name;

                        var site = SqlDBHelper.Query<Site>($"select b.* from Device as a  inner join [Site] as b on a.SiteId=b.[SID]  where a.[SID]='{pointModel.DeviceId}'").FirstOrDefault();//电站信息

                        if (site != null)
                        {
                            model.Site = site.Name;
                        }
                    }

                    //重绘图片
                    var list = newInfraredDetailsList.Where(a => a.TempId == newInfraredList[i].TempId).ToList();
                    if (Convert.ToBoolean(newInfraredList[i].IsAlarm) && list != null && list.Count > 0)
                    {
                        Bitmap image = (Bitmap)ImageHelper.GetHttpImage(newInfraredList[i].ImgBase64);
                        if (list!=null && list.Count>0)
                        {
                            for (int b = 0; b < list.Count; b++)
                            {
                                if (list[b].ToolType == "Ellipse") //椭圆区域
                                {
                                    var start = list[b].PtStart.Split(",");
                                    var end = list[b].PtEnd.Split(",");
                                    var x = Convert.ToInt32(start[0]);
                                    var y = Convert.ToInt32(start[1]);

                                    var width = Convert.ToInt32(end[0]) - Convert.ToInt32(start[0]);
                                    var height = Convert.ToInt32(end[1]) - Convert.ToInt32(start[1]);


                                    var newImg = ImageHelper.ImgDrawEllipse(image, x, y, width, height);

                                    newImg = ImageHelper.ImgDrawString(newImg, $"最高温度:{ list[b].Max}°C,最低温度:{ list[b].Min}°C", x - 10, y - 10);

                                    image = newImg;

                                }

                                if (list[b].ToolType == "Point") // 点 以椭圆形式展示
                                {
                                  
                                        var start = list[b].PtStart.Split(",");
                                        if (list[b].PtEnd.Contains(",") && list[b].PtEnd.Split(",").Count() > 0)
                                        {
                                            var end = list[b].PtEnd.Split(",");
                                            var x = Convert.ToInt32(start[0]) + 10;
                                            var y = Convert.ToInt32(start[1]) + 10;

                                            var width = Convert.ToInt32(end[0]) - Convert.ToInt32(start[0]) + 10;
                                            var height = Convert.ToInt32(end[1]) - Convert.ToInt32(start[1]) + 10;


                                            var newImg = ImageHelper.ImgDrawEllipse(image, x, y, width, height);

                                            newImg = ImageHelper.ImgDrawString(newImg, $"最高温度:{ list[b].Max}°C,最低温度:{ list[b].Min}°C", x - 10, y - 10);

                                            image = newImg;
                                        }

                                }

                                if (list[b].ToolType == "Line") // 线
                                {
                                    var start = list[b].PtStart.Split(",");
                                    var end = list[b].PtEnd.Split(",");
                                    var x = Convert.ToInt32(start[0]);
                                    var y = Convert.ToInt32(start[1]);

                                    var x1 = Convert.ToInt32(end[0]);
                                    var y1 = Convert.ToInt32(end[1]);


                                    var newImg = ImageHelper.ImaDrawLine(image, x, y, x1, y1);

                                    newImg = ImageHelper.ImgDrawString(newImg, $"最高温度:{ list[b].Max}°C,最低温度:{ list[b].Min}°C", x - 10, y - 10);

                                    image = newImg;
                                }

                                if (list[b].ToolType == "Rectangle") //矩形  
                                {
                                    var start = list[b].PtStart.Split(",");
                                    var end = list[b].PtEnd.Split(",");
                                    var x = Convert.ToInt32(start[0]);
                                    var y = Convert.ToInt32(start[1]);

                                    var width = Convert.ToInt32(end[0]) - Convert.ToInt32(start[0]);
                                    var height = Convert.ToInt32(end[1]) - Convert.ToInt32(start[1]);


                                    var newImg = ImageHelper.ImgDrawRectangle(image, x, y, width, height);

                                    newImg = ImageHelper.ImgDrawString(newImg, $"最高温度:{ list[b].Max}°C,最低温度:{ list[b].Min}°C", x - 10, y - 10);

                                    image = newImg;

                                }

                                if (list[b].ToolType == "Polygon" && !string.IsNullOrWhiteSpace(list[b].PtPoly)) // 多边形以线连成  
                                {
                                    var arr = list[b].PtPoly.Split(";");

                                    for (int c = 0; c < arr.Count(); c++)
                                    {
                                        var start = arr[c].Split(",");
                                        var end = arr[c].Split(",");
                                        var x = Convert.ToInt32(start[0]);
                                        var y = Convert.ToInt32(start[1]);

                                        var x1 = Convert.ToInt32(end[0]);
                                        var y1 = Convert.ToInt32(end[1]);


                                        var newImg = ImageHelper.ImaDrawLine(image, x, y, x1, y1);

                                        newImg = ImageHelper.ImgDrawString(newImg, $"最高温度:{ list[b].Max}°C,最低温度:{ list[b].Min}°C", x - 10, y - 10);

                                        image = newImg;
                                    }


                                }

                            }

                            var temp = image;
                            temp.Save($"{System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase}DrawImage\\{ newInfraredList[i].TempId}.jpg",
                                System.Drawing.Imaging.ImageFormat.Jpeg);

                        }
                       
                    }


                    //报警
                    if (Convert.ToBoolean(newInfraredList[i].IsAlarm) && list != null && list.Count > 0)
                    {
                        model.Img = $"{AppConfigurtaionServices.Configuration.GetSection("Draw").Value}{ newInfraredList[i].TempId}.jpg";
                        model.Time = newInfraredList[i].CREATETIME.ToString("yyyy-MM-dd HH:mm:ss");
                        model.IsAlarm = (Convert.ToBoolean(newInfraredList[i].IsAlarm) && list != null && list.Count > 0) ? "true" : "false";

                        var temperature = newInfraredList[i].AllTemp.Split(",");
                        model.Max = temperature.Max();
                        model.Min = temperature.Min();

                        result.Add(model);
                    }
                    else//正常
                    {

                        var temperature = newInfraredList[i].AllTemp.Split(",");
                        model.Max = temperature.Max();
                        model.Min = temperature.Min();

                        model.Img = $"{AppConfigurtaionServices.Configuration.GetSection("Image").Value}{ newInfraredList[i].TempId}.jpg";
                        model.Time = newInfraredList[i].CREATETIME.ToString("yyyy-MM-dd HH:mm:ss");
                        model.IsAlarm = (Convert.ToBoolean(newInfraredList[i].IsAlarm) && list != null && list.Count > 0) ? "true" : "false";

                        result.Add(model);
                    }
                  
                }
            }
            return result;
        }
    }
}
